//
//  WQEmotions.swift
//  1215-自定义键盘
//
//  Created by 王先强 on 15/12/15.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit

class WQEmotions: NSObject {
    // 表情包在bundle中的路径
    var id: String?
    //显示在toolbar中的名字
    var group_name_cn: String?
    //存放WQEmotion的数组
    var emotions = [WQEmotion]()
    
    init(dict: [String: AnyObject]) {
        super.init()
        
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        
        
        if let dictArr = dict["emoticons"] as? [[String: AnyObject]] {
            
            //创建模型标记index
            var index = 0

            for var temDict in dictArr {
                
                //判断 png = temDict["png"] 表示字典中有png键
                //判断 dir = id 表示对表情包的路径赋值成功
                
                if let png = temDict["png"] as? String, dir = id {
                    //进行字典中的png的路径进行拼接
                    temDict["png"] = dir + "/" + png
                }
            //对表情进行字典转模型
            emotions.append(WQEmotion(dict: (temDict as [String: AnyObject])))
                index++
                // 20个添加一次删除按钮
                if index == 20 {
                    emotions.append(WQEmotion(isDelete: true))
                    //清空标记
                    index = 0
                }
            }
        }
        // 根据emotions中个数，添加空白按钮
        addEmptyEmotionButtons()
        
        
    }
    
    private func addEmptyEmotionButtons() {
        // 统计每一个表情包的表情个数
        let count = emotions.count % 21
        
        if count == 0 && emotions.count > 0 {
            return
        }
        for _ in count..<20 {
            emotions.append(WQEmotion(isEmpty: true))
        }
        
        // 添加完空白后还要添加一个删除按钮
        emotions.append(WQEmotion(isDelete: true))
    }


    
    override var description: String {
        let keys = ["id","group_name_cn","emotions"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
